package com.example.bikemanagerjava.mapper;



import com.example.bikemanagerjava.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("<script>" +
            "SELECT * FROM User " +
            "<where>" +
            "  <if test='status != null'>" +
            "    status = #{status}" +
            "  </if>" +
            "  <if test='username != null and username != \"\"'>" +
            "    <if test='status != null'>AND </if>" + // 如果 status 有条件，加 AND
            "    username LIKE CONCAT('%', #{username}, '%')" +
            "  </if>" +
            "</where> " +
            "LIMIT #{skipNumber}, #{pageSize}" +
            "</script>")
    List<User> selectByPage(@Param("skipNumber") Integer skipNumber, @Param("pageSize") Integer pageSize,
                            @Param("status") Integer status,
                            @Param("username") String username);


    @Select("SELECT COUNT(*) FROM User")
    int countUsers();

    @Select("select * from User where Username like concat('%',#{username},'%')")
    List<User> selectByUserName(String username);

    @Update("update User set Username=#{username},PhoneNumber=#{phoneNumber},Email=#{email},PasswordHash=#{passwordHash}," +
            "CreatedAt=#{createdAt},Status=#{status},LastLoginAt=#{lastLoginAt},Note=#{note} where UserId=#{userId}")
    void updateUser(User user);

    @Delete("delete from User where UserId=#{userId}")
    void deleteUser(int id);

    @Select("select * from User where UserId=#{userId}")
    User selectById(int id);

    @Update("UPDATE User SET status = #{status} WHERE userId = #{id}")
    void updateStatusById( int id, int status);

    @Insert("    INSERT INTO user (username, phoneNumber, email, passwordHash, createdAt, status, lastLoginAt, note)\n" +
            "    VALUES (#{username}, #{phoneNumber}, #{email}, #{passwordHash}, #{createdAt}, #{status}, #{lastLoginAt}, #{note})")
    void insertUser(User user);
}
